package com.hskn.hss.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.hskn.hss.constant.CommonConstant;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class OssUtils {

    static BASE64Decoder decoder = new sun.misc.BASE64Decoder();

    public static String upFile(String base64String, String fileName) {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = CommonConstant.ACCESS_KEY_ID;
        String accessKeySecret = CommonConstant.ACCESS_KEY_SECRET;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        fileName = fileName.substring(0, fileName.lastIndexOf(".")) + "_" + StringUtils.uuid() + fileName.substring(fileName.lastIndexOf("."));
        fileName = fileName.replace(".png", ".jpg").replace(".jpeg", ".jpg");
        try {
            String ct = getContentType(fileName.substring(fileName.lastIndexOf(".")));
            if (StringUtils.isEmpty(ct)) {
                ossClient.putObject("sanitation-pc", "file/" + fileName, new ByteArrayInputStream(decoder.decodeBuffer(base64String)));
            } else {
                ObjectMetadata objectMetadata = new ObjectMetadata();
                objectMetadata.setContentType(ct);
                ossClient.putObject("sanitation-pc", "file/" + fileName, new ByteArrayInputStream(decoder.decodeBuffer(base64String)), objectMetadata);
            }
            // 上传Byte数组。
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 关闭OSSClient。
        ossClient.shutdown();
        return "https://sanitation-pc.oss-cn-beijing.aliyuncs.com/file/" + fileName;
    }

    public static String uploadFile(MultipartFile multipartFile) {  // filename wx_camera_1623298771160.mp4
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = CommonConstant.ACCESS_KEY_ID;
        String accessKeySecret = CommonConstant.ACCESS_KEY_SECRET;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String filename =  multipartFile.getOriginalFilename();
        try {
            InputStream multipartFileInputStream = multipartFile.getInputStream();
            ossClient.putObject("sanitation-pc","file/"+ filename, multipartFileInputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 关闭OSSClient。
        ossClient.shutdown();
        return "https://sanitation-pc.oss-cn-beijing.aliyuncs.com/file/"+filename;
    }

    public static String getContentType(String filenameExtension) {
        if (filenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (filenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (filenameExtension.equalsIgnoreCase(".jpeg") ||
                filenameExtension.equalsIgnoreCase(".jpg") ||
                filenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (filenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (filenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (filenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (filenameExtension.equalsIgnoreCase(".pptx") ||
                filenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (filenameExtension.equalsIgnoreCase(".docx") ||
                filenameExtension.equalsIgnoreCase(".doc")) {
            return "application/msword";
        }
        if (filenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "";
    }

}
